import { BusinessError, emitter } from '@kit.BasicServicesKit';
import { display, window } from '@kit.ArkUI';
import { DESTROY_FLOAT_WINDOW } from '../utils/Constants';
import { common } from '@kit.AbilityKit';
import MainAbility from "../MainAbility/MainAbility"
import { JSON } from '@kit.ArkTS';

export class WindowManager {
  static mainWindow: window.Window | undefined = undefined
  static windowStage: window.Window | undefined = undefined

  // static ShowWindow(name) {
  //   let TAG = "_:_showWindow"
  //   if (name == "index") {
  //     // window.get(WindowManager.indexId, (e,window:window.Window) => {
  //     //   console.log(TAG, "e IS", JSON.stringify(e))
  //     // this.mainWindow.show()
  //     // })
  //     //
  //     this.windowStage.setUIContent( "pages/index", (err: BusinessError, data) => {
  //       if (err.code) {
  //         console.error(TAG, 'Failed to load page. Cause: ' + JSON.stringify(err))
  //         return
  //       }
  //       console.log(TAG, 'Succeeded in loading page. Data:' + JSON.stringify(err))
  //     })
  //   }
  // }

  static CreateWindow(context: common.UIAbilityContext, name: string, sizex = 0, sizey = 0,
    backgroundColor = "rgba(0,0,0,0)", type = window.WindowType.TYPE_SCREENSHOT) {
    let TAG = "_:[createWindow] " + name
    console.log(TAG, "entered")
    let twindow: window.Window | undefined = undefined;
    let config: window.Configuration = {
      name: name,
      windowType: type,
      ctx: context
    };
    window.createWindow(config, (err: BusinessError, data: window.Window) => {
      if (err.code) {
        console.error(TAG, 'Failed to create: ' + JSON.stringify(err))
        TAG = "_:_findingHiddenWindow(if fail to create)"
        try {
          console.log(TAG, name)
          window.findWindow(name).show()
        } catch (e) {
          console.log(TAG, "error", JSON.stringify(e))
        }
        return
      }
      console.log(TAG, 'Succeeded in creating. Data: ' + JSON.stringify(data))
      twindow = data
      if (sizex != 0 || sizey != 0) {
        twindow.resize(sizex, sizey, (err: BusinessError, data) => {
          if (err.code) {
            console.error(TAG, 'Failed to resetSize. Cause: ' + JSON.stringify(err))
            return
          }
          console.log(TAG, 'Succeeded in resetting size. Data:' + JSON.stringify(err))
        })
      }
      twindow.setUIContent('pages/' + name, (err: BusinessError, data) => {
        if (err.code) {
          console.error(TAG, 'Failed to load page. Cause: ' + JSON.stringify(err))
          return
        }
        console.log(TAG, 'Succeeded in loading page. Data:' + JSON.stringify(err))
        if (backgroundColor != "") {
          twindow && twindow.setWindowBackgroundColor('#00ffffff')
        }
        twindow && twindow.showWindow((err: BusinessError, data) => {
          if (err.code) {
            console.error(TAG, 'Failed to show window. Case: ' + JSON.stringify(err))
            return
          }
          console.log(TAG, 'Succeeded in showing the window. Data:' + JSON.stringify(err))
        })
      })
      const displayClass = display.getDefaultDisplaySync();
      console.log(TAG, "default displayClass", displayClass.name)
      window.minimizeAll(displayClass.id);
      emitter.once({ eventId: DESTROY_FLOAT_WINDOW }, () => {
        console.log(TAG, 'emitter once')
        twindow && twindow.destroyWindow(() => {
          if (err.code) {
            console.error(TAG, 'Failed to destroy the window. Cause:' + JSON.stringify(err))
            return
          }
          console.log(TAG, 'Succeeded to destroy the window. Data:' + JSON.stringify(data))
        })
      })
    })
  }

  static async DestroyWindow(name: string) {
    let TAG = "_:_DestroyWindow " + name
    try {
      await window.findWindow(name).destroyWindow()
      console.log(TAG, 'destroy the window.' + name)
    } catch (e) {
      console.log(TAG, "find window fail", JSON.stringify(e))
    }
  }


  // static async CreateFloatWin(context: common.UIAbilityContext) {
  //   let TAG = "_:_[enterFloat]"
  //   console.log(TAG, "entered")
  //   let windowClass: window.Window | undefined = undefined;
  //   let config: window.Configuration = {
  //     name: 'floatWindow',
  //     windowType: window.WindowType.TYPE_SCREENSHOT,
  //     ctx: context
  //   };
  //   window.createWindow(config, (err: BusinessError, data: window.Window) => {
  //     if (err.code) {
  //       console.error(TAG, 'Failed to create the float: ' + JSON.stringify(err))
  //       window.findWindow("floatWindow").show()
  //       return
  //     }
  //     console.log(TAG, 'Succeeded in creating the float. Data: ' + JSON.stringify(data))
  //     windowClass = data
  //
  //     windowClass.setUIContent('pages/floatWindow', (err: BusinessError, data) => {
  //       if (err.code) {
  //         console.error(TAG, 'Failed to load floatWindow page. Cause: ' + JSON.stringify(err))
  //         return
  //       }
  //       console.log(TAG, 'Succeeded in loading ScreenRecorder page. Data:' + JSON.stringify(err))
  //       windowClass && windowClass.setWindowBackgroundColor('#00ffffff')
  //       windowClass && windowClass.showWindow((err: BusinessError, data) => {
  //         if (err.code) {
  //           console.error(TAG, 'Failed to show window. Case: ' + JSON.stringify(err))
  //           return
  //         }
  //         console.log(TAG, 'Succeeded in showing the window. Data:' + JSON.stringify(err))
  //       })
  //     })
  //     const displayClass = display.getDefaultDisplaySync();
  //     window.minimizeAll(displayClass.id);
  //     emitter.once({ eventId: DESTROY_FLOAT_WINDOW }, () => {
  //       console.log(TAG, 'emitter once')
  //       windowClass && windowClass.destroyWindow((err: BusinessError, data) => {
  //         if (err.code) {
  //           console.error(TAG, 'Failed to destroy the window. Cause:' + JSON.stringify(err))
  //           return
  //         }
  //         console.log(TAG, 'Succeeded to destroy the window. Data:' + JSON.stringify(err))
  //       })
  //     })
  //   })
  // }

  static async fullSizeWindow(winName) {
    let TAG = "_:floatRecall_ClickFullScreen"
    console.log(TAG, "entered")
    try {
      let fwindow = window.findWindow(winName)
      console.log(TAG, "window found", JSON.stringify(fwindow.getWindowProperties()))
      // await fwindow.loadContent("pages/indexv")
      console.log(TAG, "load window content success")
      await fwindow.moveWindowTo(0, 0)
      console.log(TAG, "move window success")
      await fwindow.resize(720, 1280)
      console.log(TAG, "resize window success")
      await fwindow.show()
      console.log(TAG, "show window success")
    } catch (e) {
      console.error(TAG, JSON.stringify(e))
    }
  }

  static async smallSizeWindow(winName, NavX, NavY, NavW, WinH) {
    let TAG = "_:floatRecall_ClickNormalFloatWindow"
    console.log(TAG, "entered")
    try {
      let fwindow = window.findWindow(winName)
      console.log(TAG, "window found", JSON.stringify(fwindow.getWindowProperties()))
      console.log(TAG, "load window content success")
      await fwindow.moveWindowTo(NavX, NavY)
      console.log(TAG, "move window success")
      await fwindow.resize(NavW, WinH)
      console.log(TAG, "resize window success")
      await fwindow.show()
      console.log(TAG, "show window success")
    } catch (e) {
      console.log(TAG, JSON.stringify(e))
    }
  }
}